package xiao_jin.fantastic_journey.api.capability;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import xiao_jin.fantastic_journey.FantasticJourney;
import xiao_jin.fantastic_journey.util.ModUtil;

public class CapabilityPayload implements CustomPayload {
    public static final Id<CapabilityPayload> PAYLOAD_ID = new Id<>(ModUtil.getIdOfMod( "capability"));

    private final NbtCompound nbt;
    private final Identifier capName;

    public CapabilityPayload(Capability capability) {
        this.capName = capability.id;
        this.nbt = new NbtCompound();

        capability.writeInto(this.nbt);
    }

    public CapabilityPayload(NbtCompound nbt, Identifier capName) {
        this.nbt = nbt;
        this.capName = capName;
    }

    public NbtCompound getNbt() {
        return this.nbt;
    }

    public Identifier getCapName() {
        return this.capName;
    }

    @Override
    public Id<CapabilityPayload> getId() {
        return PAYLOAD_ID;
    }

    public static final PacketCodec<PacketByteBuf, CapabilityPayload> CODEC = new PacketCodec<>() {
        @Override
        public CapabilityPayload decode(PacketByteBuf buf) {
            return new CapabilityPayload(buf.readNbt(), buf.readIdentifier());
        }

        @Override
        public void encode(PacketByteBuf buf, CapabilityPayload value) {
            buf.writeNbt(value.nbt);
            buf.writeIdentifier(value.capName);
        }
    };
}